package org.example.mulitithreading.ex5;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Demo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //线程开启之后需要执行里面的call方法
        MyCallable mc = new MyCallable();
        //可以获取线程执行完毕之后的结果.也可以作为参数传递给Thread对象
        FutureTask<String> ft = new FutureTask<String>(mc);

        //创建线程对象
        Thread t1 = new Thread(ft);
        //开启线程
        System.out.println("提交任务之前 "+getStringDate());
        t1.start();
        System.out.println("提交任务之后 "+getStringDate());
        Thread.sleep(4000);
        System.out.println("已经睡了4秒,开始获取结果 "+getStringDate());
        System.out.println("获取返回值: "+ft.get());
        System.out.println("获取到结果之后 "+getStringDate());

//        String s = ft.get();
//        System.out.println(s);
    }

    public static String getStringDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        return formatter.format(currentTime);
    }
}
